package com.dhcc.cdc.busi.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.CdcLabSetMapper;
import com.dhcc.cdc.busi.entity.CdcLabOrderEntity;
import com.dhcc.cdc.busi.entity.CdcLabSetEntity;
import com.dhcc.cdc.busi.entity.CdcOrdexecEntity;
import com.dhcc.cdc.busi.service.ICdcLabOrderService;
import com.dhcc.cdc.busi.service.ICdcLabSetService;
import com.dhcc.cdc.busi.service.ICdcOrdexecService;
import com.dhcc.cdc.busi.service.ICdcOrdmastService;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检验组套表数据服务层接口
 * 
 * @ClassName: LabSetServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-04-26 16:21:40
 */
@Service("labSetService")
public class CdcLabSetServiceImpl extends BaseServiceImpl<CdcLabSetMapper, CdcLabSetEntity> implements ICdcLabSetService {
    @Autowired
    ICdcOrdmastService ordmastSrv;
    @Autowired
    ICdcLabOrderService labordSrv;
    @Autowired
    private ICdcOrdexecService ordexecService;

	@Override
	public List<CdcLabSetEntity> page(Page<CdcLabSetEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<CdcLabSetEntity> list(Map<String, Object> map) {
		return warpLabSet(this.baseMapper.list(map));
	}

    @Override
    public List<Long> selectLabVisitIdByLabOrder(List<Long> laborderIds) {
        return this.baseMapper.selectLabVisitIdByLabOrder(laborderIds);
    }

    @Override
    public List<CdcLabSetEntity> selectByAdmDate(String aFromDate, String aToDate) {
        return this.baseMapper.selectByAdmDate(aFromDate, aToDate);
    }

    public List<CdcLabSetEntity> warpLabSet(List<CdcLabSetEntity> list){
        Map<Long, Object> cacheDic = new HashMap<Long, Object>();
        for (CdcLabSetEntity labset : list) {
            if (labset.getOrderExecId() != null) {
                if (cacheDic.containsKey(labset.getOrderExecId())) {
                    labset.setOrderExec((CdcOrdexecEntity) cacheDic.get(labset.getOrderExecId()));
                } else {
                    CdcOrdexecEntity ordexecEntity = ordexecService.selectById(labset.getOrderExecId());
                    labset.setOrderExec(ordexecEntity);
                    cacheDic.put(labset.getOrderExecId(), ordexecEntity);
                }
            } else {
                labset.setOrderExec(new CdcOrdexecEntity());
            }
            if (labset.getCtLabOrderId() != null) {
                if (cacheDic.containsKey(labset.getCtLabOrderId())) {
                    labset.setOrderLab((CdcLabOrderEntity) cacheDic.get(labset.getCtLabOrderId()));
                } else {
                    CdcLabOrderEntity ordermast = this.labordSrv.selectById(labset.getCtLabOrderId());
                    labset.setOrderLab(ordermast);
                    cacheDic.put(labset.getCtLabOrderId(), ordermast);
                }
            } else {
                labset.setOrderLab(new CdcLabOrderEntity());
            }
        }
        return list;
    }

}